package com.xm.controller.web;

import com.xm.pojo.Valve;
import com.xm.service.ValveService;
import com.xm.util.JSONResult;
import com.xm.util.page.PageUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;

/**
 * Created by wzl on 2018/4/11.
 */
@Controller
@RequestMapping("/web/valve")
public class ValveController {
    @Resource
    private ValveService valveService;


    /**
     * 新增阀门方法
     *
     * @return jsonResult
     * @throws Exception
     */
    @RequestMapping(value = "/insertValve", method = RequestMethod.POST)
    @ResponseBody
    public JSONResult insertValve(Valve valve) throws Exception {
        JSONResult jsonResult = new JSONResult();
        valveService.insert(valve);
        jsonResult.setSuccess(true);
        return jsonResult;
    }

    /**
     * 删除阀门方法
     *
     * @return jsonResult
     * @throws Exception
     */
    @RequestMapping(value = "/deleteValve", method = RequestMethod.POST)
    @ResponseBody
    public JSONResult deleteValve(Valve valve) throws Exception {
        JSONResult jsonResult = new JSONResult();
        valveService.delete(valve.getId());
        jsonResult.setSuccess(true);
        return jsonResult;
    }

    /**
     * 修改阀门方法
     *
     * @return jsonResult
     * @throws Exception
     */
    @RequestMapping(value = "/updateValve", method = RequestMethod.POST)
    @ResponseBody
    public JSONResult updateValve(Valve valve) throws Exception {
        JSONResult jsonResult = new JSONResult();
        valveService.update(valve);
        jsonResult.setSuccess(true);
        return jsonResult;
    }

    /**
     * 查询全部阀门方法
     *
     * @param valve
     * @return
     */
    @RequestMapping(value = "/selectValveList", method = RequestMethod.GET)
    @ResponseBody
    public JSONResult selectValveList(Valve valve) {
        JSONResult jsonResult = new JSONResult();
        List<Valve> list = valveService.selectList(valve);
        jsonResult.setData(list);
        jsonResult.setSuccess(true);
        return jsonResult;
    }

    /**
     * 查询阀门详情方法
     *
     * @param valve
     * @return
     */
    @RequestMapping(value = "/selectValveDetail", method = RequestMethod.GET)
    @ResponseBody
    public JSONResult selectValveDetail(Valve valve) {
        JSONResult jsonResult = new JSONResult();
        Valve valveInfo = valveService.selectOne(valve);
        jsonResult.setData(valveInfo);
        jsonResult.setSuccess(true);
        return jsonResult;
    }

    /**
     * 分页查询全部阀门方法
     *
     * @param valve
     * @return
     */
    @RequestMapping(value = "/selectValvePage", method = RequestMethod.GET)
    @ResponseBody
    public JSONResult selectValvePage(Valve valve) {
        JSONResult jsonResult=new JSONResult();
        List<Valve> valveList=valveService.selectPageList(valve);
        int total=valveService.queryTotal(valve);
        int length=valve.getLength();
        int start=valve.getStart();
        PageUtils pager = new PageUtils(valveList, total, length,start);
        jsonResult.setData(pager);
        jsonResult.setSuccess(true);
        return jsonResult;
    }

    /**
     * 批量删除阀门
     */
    @RequestMapping(value ="/deleteValves", method = RequestMethod.POST)
    @ResponseBody
    public JSONResult deleteValves(@RequestParam(value = "ids[]") Integer[] ids) {
        JSONResult jsonResult=new JSONResult();
        for(int i=0;i<ids.length;i++){
            valveService.delete(ids[i]);
        }
        jsonResult.setSuccess(true);
        return jsonResult;
    }

}
